package com.sdhs.common.utils;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.sdhs.common.core.domain.JSONResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/*项目交工，项目决算推送营销工具类*/
@Log4j2
@Component
public class SafetyUtil {

    private static String PUSH_URL;

    @Value("${safetying.url}")
    public void setPushUrl(String pushUrl) {
        PUSH_URL = pushUrl;
        System.out.println(PUSH_URL+ "推送地址PUSH_URLPUSH_URL");
    }

    public static String pushUrl(String data) {
        String code = "500";
        try {
            System.out.println(PUSH_URL+ "安全系统推送地址PUSH_URLPUSH_URL");
            HttpResponse execute = HttpRequest.post(PUSH_URL).header("Content-Type", "application/json; charset=utf-8").body(data).execute();
            System.out.println(PUSH_URL+ "安全系统推送地址PUSH_URLPUSH_URL");
            log.info( "响应内容：{}", execute.body());

            if (StringUtils.isNotEmpty(execute.body())) {
                JSONResult bean = JSONUtil.toBean(execute.body(), JSONResult.class);
                if (bean.getCode() == 200) {
                    code = String.valueOf(bean.getCode());
                    log.info("安全系统推送成功" + execute.body());
                    System.out.println("安全系统推送成功" + execute.body());
                } else {
                    log.info("安全系统推送失败" + execute.body());
                    System.out.println("安全系统推送失败" + execute.body());
                }
            } else {
                log.info("安全系统接口访问成功但是推送失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("推送安全系统失败，请联系管理员");
        }
        return code;
    }

}
